package com.wzh.musichome.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wzh.musichome.common.ConstantCode;
import com.wzh.musichome.common.ResponseTemplate;
import com.wzh.musichome.entity.dto.FrontUserLoginDTO;
import com.wzh.musichome.entity.pojo.FrontUser;
import com.wzh.musichome.mapper.FrontUserMapper;
import com.wzh.musichome.service.FrontUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wzh.musichome.utils.JwtUtil;
import io.jsonwebtoken.Claims;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;

/**
 * <p>
 * 前端用户 服务实现类
 * </p>
 *
 * @author wzh
 * @since 2020-12-10
 */
@Service
public class FrontUserServiceImpl extends ServiceImpl<FrontUserMapper, FrontUser> implements FrontUserService {
    @Autowired
    private FrontUserMapper frontUserMapper;

    @Autowired
    private JwtUtil JwtUtil;
//    @Override
//    public Object deleteConsumer(int id) {
//        return consumerMapper.deleteConsumer(id);
//    }


    @Override
    public Object login(FrontUserLoginDTO frontUserLoginDTO) {
        String username = frontUserLoginDTO.getUsername();
        String password = frontUserLoginDTO.getPassword();
        //检测用户名和密码是否为空
        if (StringUtils.isBlank(username) || StringUtils.isBlank(password)) {
            return ResponseTemplate.getFailureResponse(ConstantCode.FAILURE_LOGIN_USERNAME_PASSWORD_EMPTY, "用户名/密码不能为空");
        }

        QueryWrapper<FrontUser> wrapper = new QueryWrapper<>();
        wrapper.eq("username",username);
        FrontUser frontUser = frontUserMapper.selectOne(wrapper);
        if (frontUser == null){
            return ResponseTemplate.getFailureResponse(ConstantCode.FAILURE_LOGIN_USER_NULL, "用户不存在");
        }
        if (!password.matches(frontUser.getPassword())){
            System.out.println(frontUser.getPassword());
            return ResponseTemplate.getFailureResponse(ConstantCode.FAILURE_LOGIN_PASSWORD_WRONG, "密码错误");
        }

        //生成token
        String jwt = JwtUtil.createJWT(frontUser.getId()+"", frontUser.getUsername(),true);
        HashMap<String, Object> map = new HashMap<>();
        map.put("flag",true);
        map.put("code","0000");
        map.put("data", frontUser);
        map.put("token",jwt);
        return map ;
    }

    @Override
    public Object getConsumerInfo(String token) {
        //解析令牌
        Claims claims = JwtUtil.paresJWT(token);
        if (claims == null || StringUtils.isBlank(claims.getSubject())){
            return ResponseTemplate.getFailureResponse(ConstantCode.FAILURE_LOGIN_TOKEN_FAILURE, "获取token失败");
        }
        String username = claims.getSubject();
        System.out.println(claims.toString());
        QueryWrapper<FrontUser> wrapper = new QueryWrapper<>();
        wrapper.eq("username",username);
        FrontUser frontUser = frontUserMapper.selectOne(wrapper);
        if (frontUser == null){
            return ResponseTemplate.getFailureResponse(ConstantCode.FAILURE_LOGIN_USER_NULL, "用户不存在");
        }
        frontUser.setPassword(null);
        ResponseTemplate.ResultData data = ResponseTemplate.getSuccessResponse();
        data.setData(frontUser);
        data.setMsg("token解析成功");
        return data ;
    }
}
